package com.zxzhu.joyread.View;

import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.avos.avoscloud.AVOSCloud;
import com.bumptech.glide.Glide;
import com.zxzhu.joyread.Adapter.MyFragmentPagerAdapter;
import com.zxzhu.joyread.Common.BaseActivity;
import com.zxzhu.joyread.Presenter.IMainPresenter;
import com.zxzhu.joyread.Presenter.MainPresenter;
import com.zxzhu.joyread.R;
import com.zxzhu.joyread.View.fragments.MineView;
import com.zxzhu.joyread.View.fragments.ShelfView;
import com.zxzhu.joyread.View.fragments.ShopView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainView extends BaseActivity implements IMainView, BottomNavigationBar.OnTabSelectedListener, ViewPager.OnPageChangeListener {

    private IMainPresenter presenter = new MainPresenter(this);
    private NavigationView navigationView;
    private BottomNavigationBar bar;
    private ViewPager viewPager;
    private List<Fragment> fragments;
    private TextView title,mName;
    private ImageView search,side;
    private DrawerLayout drawerLayout;
    private CircleImageView mIcon;
    private String nickname,iconUrl,gender,id;

    @Override
    public void initState() {
//        super.initState();
    }

    @Override
    protected void initData() {
//        tv1 = $(R.id.nickname);
//        tv2 = $(R.id.gender);
        bar = $(R.id.navigation_bar);
//        icon = $(R.id.icon);
        presenter.setText();
        viewPager = $(R.id.viewPager);
        navigationView = $(R.id.nav_view);
        title = $(R.id.header_title);
        search = $(R.id.header_search);
        side = $(R.id.side);
        drawerLayout = $(R.id.drawer_layout);
        navigationView.setItemIconTintList(null);
        fragments = new ArrayList<>();
        // presenter.gotoView();//根据是否登录跳转界面
        setData();
        setViewPager();
        setBottomBar();
        openDrawer();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void setData() {

        setNavigationView();

        Intent intent = getIntent();
        JSONObject userMes = null;
        //传过来的微博或qq资料
        nickname = intent.getStringExtra("name");
        gender = intent.getStringExtra("gender");
        iconUrl = intent.getStringExtra("iconUrl");
        id = intent.getStringExtra("id");
        Log.d("000000", "setData: "+iconUrl);
        Toast.makeText(MainView.this, "你好," + nickname, Toast.LENGTH_LONG).show();
    }




    private void setNavigationView() {
        navigationView = $(R.id.nav_view);
        navigationView.setItemIconTintList(null);
        View view = navigationView.getHeaderView(0);
        mIcon = (CircleImageView) view.findViewById(R.id.icon);
        mName = (TextView) view.findViewById(R.id.nickname);
        Glide.with(this).load(iconUrl).into(mIcon);
        mName.setText(nickname);

    }


    @Override
    public void setBottomBar() {
        bar.setActiveColor(R.color.md_green_400)
                .setInActiveColor(R.color.md_blue_grey_500)
                .addItem(new BottomNavigationItem(R.drawable.shelf,"书架"))
                .addItem(new BottomNavigationItem(R.drawable.shop, "书城"))
                .addItem(new BottomNavigationItem(R.drawable.mine, "个人中心"))
                //设置第一个为默认
                .setFirstSelectedPosition(0)
                .initialise();
        //mBottomNavigationBar.setAutoHideEnabled(true);
        bar.setTabSelectedListener(this);

    }


    @Override
    public void setViewPager() {
        fragments.add(new ShelfView());
        fragments.add(new ShopView());
        fragments.add(new MineView());
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),fragments));
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(this);
    }

    @Override
    public void openDrawer() {
        side.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        bar.selectTab(position);
        switch (position) {
            case 0:
                title.setText("书架");
                search.setVisibility(View.VISIBLE);
                break;
            case 1:
                title.setText("书城");
                search.setVisibility(View.VISIBLE);
                break;
            case 2:
                title.setText("个人中心");
                search.setVisibility(View.INVISIBLE);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onTabSelected(int position) {
        viewPager.setCurrentItem(position);
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }
}
